{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Common Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: commonChars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #查找共用字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "给你一个字符串数组 <code>words</code> ，请你找出所有在 <code>words</code> 的每个字符串中都出现的共用字符（ <strong>包括重复字符</strong>），并以数组形式返回。你可以按 <strong>任意顺序</strong> 返回答案。\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>words = [\"bella\",\"label\",\"roller\"]\n",
    "<strong>输出：</strong>[\"e\",\"l\",\"l\"]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>words = [\"cool\",\"lock\",\"cook\"]\n",
    "<strong>输出：</strong>[\"c\",\"o\"]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length &lt;= 100</code></li>\n",
    "\t<li><code>words[i]</code> 由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-common-characters](https://leetcode.cn/problems/find-common-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-common-characters](https://leetcode.cn/problems/find-common-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"bella\",\"label\",\"roller\"]', '[\"cool\",\"lock\",\"cook\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # ans = []\n",
    "        # words.sort(key = len)\n",
    "        # for i in words[0]:\n",
    "        #     for j in words:\n",
    "        #         if words[0].count(i) <= j.count(i):\n",
    "        #             if j == words[-1]:\n",
    "        #                 ans.append(i)\n",
    "        #             else:\n",
    "        #                 continue\n",
    "        #         else:\n",
    "        #             break\n",
    "        # return ans\n",
    "        dict1 = dict()\n",
    "        words.sort(key = len)\n",
    "        for i in words[0]:\n",
    "            dict1[i] = words[0].count(i)\n",
    "        for j in words:\n",
    "            for k in dict1.keys():\n",
    "                if j.count(k) < dict1[k]:\n",
    "                    dict1[k] = j.count(k)\n",
    "        ans = []\n",
    "        for i in dict1:\n",
    "            if dict1 == 0:\n",
    "                dict1.pop(i)\n",
    "            else:\n",
    "                for j in range(dict1[i]):\n",
    "                    ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        tmp = Counter(words[0])\n",
    "        for w in words[1:]:\n",
    "            tmp = tmp&Counter(w)\n",
    "        res = []\n",
    "        for k,v in tmp.items():\n",
    "            res.extend([k]*v)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        temp = []\n",
    "        ret = list(words[0])\n",
    "        l = len(words)\n",
    "        for i in range(l):\n",
    "            if i == 0:\n",
    "                continue\n",
    "            for j in words[i]:\n",
    "                if j in ret:\n",
    "                    ret.remove(j)\n",
    "                    temp.append(j)\n",
    "            ret = temp\n",
    "            temp = []\n",
    "        return ret\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        res = [0 for i in range(26)]\n",
    "        for i in range(len(words[0])):\n",
    "            index = ord(words[0][i]) - ord('a')\n",
    "            res[index] += 1\n",
    "        for word in words:\n",
    "            d = {}\n",
    "            for i in range(len(word)):\n",
    "                if word[i] in d:\n",
    "                    d[word[i]] += 1\n",
    "                else:\n",
    "                    d[word[i]] = 1\n",
    "            for i in range(26):\n",
    "                c = chr(ord('a')+i)\n",
    "                if c in d:\n",
    "                    res[i] = min(res[i],d[c])\n",
    "                else:\n",
    "                    res[i] = 0\n",
    "        ans = []   \n",
    "        for i in range(len(res)):\n",
    "            for j in range(res[i]):\n",
    "                c = chr(ord('a')+i)\n",
    "                ans.append(c)\n",
    "        return ans\n",
    "        \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        com = set(words[0])\n",
    "        for i in range(1, len(words)):\n",
    "            com =  com & set(words[i])\n",
    "            if not com:\n",
    "                return []\n",
    "        hashtable = {}\n",
    "        for ch in com:\n",
    "            tmp = float(\"inf\")\n",
    "            for word in words:\n",
    "                word_cnt = Counter(word)\n",
    "                if word_cnt[ch] < tmp:\n",
    "                    tmp = word_cnt[ch]\n",
    "                    hashtable[ch] = word_cnt[ch]\n",
    "        ans = \"\"\n",
    "        for i in hashtable:\n",
    "            ans += i*hashtable[i]\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        c = Counter(words[0])\n",
    "        for w in words[1:]:\n",
    "            c &= Counter(w)\n",
    "        return list(c.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if len(words) == 1:\n",
    "            print(words[0])\n",
    "        index_base = defaultdict(int)\n",
    "        index = defaultdict(int)\n",
    "        for i in words[0]:\n",
    "            index_base[i] = index_base[i] + 1\n",
    "        for word in words:\n",
    "            index = defaultdict(int)\n",
    "            for k in word:\n",
    "                index[k] = index[k] + 1\n",
    "            for num in index_base:\n",
    "                index_base[num] = min(index_base[num],index[num])\n",
    "    \n",
    "        res = ''\n",
    "        for key in index_base:\n",
    "            res = res + key * index_base[key]\n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        return list(reduce(lambda a, b: a & b, map(Counter, words)).elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        a = words[0]\n",
    "        res = []\n",
    "        b = []\n",
    "        for k in words:\n",
    "            b.append(list(k))\n",
    "\n",
    "        words = b\n",
    "        if len(words) == 1:\n",
    "            return res\n",
    "\n",
    "        for s in a:\n",
    "            exist = 1\n",
    "            for i in range(0, len(words)):\n",
    "                if s not in words[i]:\n",
    "                    exist = 0\n",
    "                    break\n",
    "                else:\n",
    "                    words[i].remove(s)\n",
    "            if exist == 1:\n",
    "                res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minfreq = [float(\"inf\")] * 26\n",
    "\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord(\"a\")] += 1\n",
    "            for i in range(26):\n",
    "                minfreq[i] = min(minfreq[i], freq[i])\n",
    "       \n",
    "        res = []\n",
    "        for i in range(26):\n",
    "            res.extend([chr(i + ord('a'))] * minfreq[i])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        tmp = collections.Counter(words[0])\n",
    "        l = []\n",
    "        for i in range(1, len(words)):\n",
    "            tmp = tmp & collections.Counter(words[i])\n",
    "\n",
    "        for k in tmp:\n",
    "            v = tmp[k]\n",
    "            while v:\n",
    "                l.append(k)\n",
    "                v -= 1\n",
    "\n",
    "        return l\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minfreq = [float(\"inf\")] * 26\n",
    "\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord(\"a\")] += 1\n",
    "            for i in range(26):\n",
    "                minfreq[i] = min(minfreq[i], freq[i])\n",
    "       \n",
    "        res = []\n",
    "        for i in range(26):\n",
    "            res.extend([chr(i + ord('a'))] * minfreq[i])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minFreq = [0] * 26\n",
    "        for c in words[0]:\n",
    "            minFreq[ord(c) - ord('a')] += 1\n",
    "        \n",
    "\n",
    "        for i in range(1,len(words)):\n",
    "            freq = [0] * 26\n",
    "\n",
    "            for c in words[i]:\n",
    "                freq[ord(c) - ord('a')] += 1\n",
    "            \n",
    "            for j in range(26):\n",
    "                minFreq[j] = min(minFreq[j],freq[j])\n",
    "        \n",
    "        res = []\n",
    "        for i in range(26):\n",
    "            for j in range(minFreq[i]):\n",
    "                res.append(chr(i+ord('a')))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # 用数组来记录每个字母出现的最小次数\n",
    "        # 用第一个字符串的字符来初始化一开始的hash数组\n",
    "        if not words:\n",
    "            return []\n",
    "        hash_table = [0] * 26\n",
    "        for i in words[0]:\n",
    "            hash_table[ord(i) - ord('a')] += 1\n",
    "        for j in range(1,len(words)):\n",
    "            hash_tmp = [0] * 26\n",
    "            for s in words[j]:\n",
    "                hash_tmp[ord(s) - ord('a')] += 1\n",
    "            for i in range(26):\n",
    "                hash_table[i] = min(hash_tmp[i],hash_table[i])\n",
    "        \n",
    "        result = []\n",
    "        for i in range(26):\n",
    "            while hash_table[i]>0:\n",
    "                result.append(chr(i + ord('a')))\n",
    "                hash_table[i] -= 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        n = len(words)\n",
    "        common = {}\n",
    "        for char in words[0]:\n",
    "            common[char] = common.get(char,0) + 1\n",
    "        for i in range(1,n):\n",
    "            cur = {}\n",
    "            for char in words[i]:\n",
    "                if char in common:\n",
    "                    cur[char] = cur.get(char,0) + 1\n",
    "            for key in common:\n",
    "                if key in cur:\n",
    "                    common[key] = min(common[key],cur[key])\n",
    "                else:\n",
    "                    common[key] = 0\n",
    "                \n",
    "        res = []\n",
    "        for key in common:\n",
    "            count = common[key]\n",
    "            for _ in range(count):\n",
    "                res.append(key)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        cnt_dict = {} # (char, relative_idx):count\n",
    "        for ith, s in enumerate(words):\n",
    "            char_cnt = {}\n",
    "            for jth,char in enumerate(s):\n",
    "                if char not in char_cnt:\n",
    "                    char_cnt[char] = 1\n",
    "                else:\n",
    "                    char_cnt[char] += 1\n",
    "                \n",
    "                item = (char, char_cnt[char])\n",
    "                \n",
    "                if item not in cnt_dict:\n",
    "                    cnt_dict[item] = 1\n",
    "                else:\n",
    "                    cnt_dict[item] += 1\n",
    "        \n",
    "        return [item[0] for item in cnt_dict if cnt_dict[item] == len(words)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        ht = [0] * 26\n",
    "        res = []\n",
    "\n",
    "        for i in range(len(words[0])):\n",
    "            ht[ord(words[0][i])-97] += 1\n",
    "        \n",
    "        for i in range(1, len(words)):\n",
    "            word = words[i]\n",
    "            ht_tmp = [0] * 26\n",
    "            for w in word:\n",
    "                ht_tmp[ord(w)-97] += 1\n",
    "            \n",
    "            for j in range(len(ht)):\n",
    "                ht[j] = min(ht[j], ht_tmp[j])\n",
    "        \n",
    "        for i in range(len(ht)):\n",
    "            while ht[i] > 0:\n",
    "                res.append(chr(i+97))\n",
    "                ht[i] -= 1\n",
    "        return res\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minFreq = [float(\"inf\")] * 26\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord('a')] += 1\n",
    "            for i in range(26):\n",
    "                minFreq[i] = min(minFreq[i], freq[i])\n",
    "        ans = []\n",
    "        for i in range(26):\n",
    "            ans.extend([chr(i + ord('a'))] * minFreq[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if not words:\n",
    "            return\n",
    "        result = []\n",
    "        hash = [0] * 26 # 用来记录26个字符里出现的最小频率\n",
    "        # 用第一个字符串来初始化hash\n",
    "        for i, c in enumerate(words[0]):\n",
    "            hash[ord(c) - ord('a')] += 1\n",
    "\n",
    "        for i in range(1, len(words)):\n",
    "            hashOther = [0] * 26\n",
    "            for j in range(len(words[i])):\n",
    "                hashOther[ord(words[i][j]) - ord('a')] += 1\n",
    "            for k in range(26):\n",
    "                hash[k] = min(hash[k], hashOther[k])\n",
    "        for i in range(26):\n",
    "            while hash[i] != 0:\n",
    "                result.extend(chr(i + ord('a')))\n",
    "                hash[i] -= 1\n",
    "        return result                       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        l = []\n",
    "        tmp = collections.Counter(words[0])\n",
    "        for i in range(1,len(words)):\n",
    "            tmp = tmp&collections.Counter(words[i])\n",
    "        for j in tmp:\n",
    "            v = tmp[j]\n",
    "            while v:\n",
    "                l.append(j)\n",
    "                v -= 1\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        n = len(words)\n",
    "        cnt = Counter(words[0])\n",
    "        for i in range(1, n):\n",
    "            cnt &= Counter(words[i])\n",
    "        \n",
    "        res = []\n",
    "        for k in cnt:\n",
    "            while cnt[k]:\n",
    "                res.append(k)\n",
    "                cnt[k] -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        tmp = collections.Counter(words[0])\n",
    "        L=[]\n",
    "        for i in range(1,len(words)):\n",
    "            tmp = tmp & collections.Counter(words[i])\n",
    "        for j in tmp:\n",
    "            while tmp[j]>0:\n",
    "                L.append(j)\n",
    "                tmp[j] -=1\n",
    "        return L\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        if not words:\n",
    "            return []\n",
    "        if len(words) < 2:\n",
    "            return list(words[0])\n",
    "        for ch in words[0]:\n",
    "            if all(ch in words[i] for i in range(len(words))):\n",
    "                ans.append(ch)\n",
    "                words = [i.replace(ch,'',1)  for i in words]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if not words: return []\n",
    "        result = []\n",
    "        hash = [0] * 26 # 用来统计所有字符串里字符出现的最小频率\n",
    "        for i, c in enumerate(words[0]):  # 用第一个字符串给hash初始化\n",
    "            hash[ord(c) - ord('a')] += 1\n",
    "        # 统计除第一个字符串外字符的出现频率\n",
    "        for i in range(1, len(words)):\n",
    "            hashOtherStr = [0] * 26\n",
    "            for j in range(len(words[i])):\n",
    "                hashOtherStr[ord(words[i][j]) - ord('a')] += 1\n",
    "            # 更新hash，保证hash里统计26个字符在所有字符串里出现的最小次数\n",
    "            for k in range(26):\n",
    "                hash[k] = min(hash[k], hashOtherStr[k])\n",
    "        # 将hash统计的字符次数，转成输出形式\n",
    "        for i in range(26):\n",
    "            while hash[i] != 0: # 注意这里是while，多个重复的字符\n",
    "                result.extend(chr(i + ord('a')))\n",
    "                hash[i] -= 1\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        last = defaultdict(int)\n",
    "        for c in words[0]:\n",
    "            last[c] += 1\n",
    "\n",
    "        for word in words[1:]:\n",
    "            cur = defaultdict(int)\n",
    "            for c in word:\n",
    "                if cur[c]<last[c]: #保留最小的次数\n",
    "                    cur[c] += 1\n",
    "            last = cur\n",
    "        rs = []\n",
    "        for k,v in last.items():\n",
    "            for i in range(v):\n",
    "                rs.append(k)\n",
    "        return rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        ans = None\n",
    "        for word in words:\n",
    "            a = Counter(word)\n",
    "            if ans==None:\n",
    "                ans = a \n",
    "            else:\n",
    "                ans = ans&a \n",
    "        return list(ans.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        def get_letter_to_count(word: str) -> dict:\n",
    "            letter_to_count = defaultdict(int)\n",
    "            for letter in word:\n",
    "                letter_to_count[letter] += 1\n",
    "            return letter_to_count\n",
    "        letter_to_counts = [get_letter_to_count(word) for word in words]\n",
    "        res = []\n",
    "        for i in range(ord('a'), ord('z') + 1):\n",
    "            min_ = float('inf')\n",
    "            for letter_to_count in letter_to_counts:\n",
    "                min_ = min([min_, letter_to_count[chr(i)]])\n",
    "            res.extend([chr(i) for _ in range(min_)])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # ans = []\n",
    "        # words.sort(key = len)\n",
    "        # for i in words[0]:\n",
    "        #     for j in words:\n",
    "        #         if words[0].count(i) <= j.count(i):\n",
    "        #             if j == words[-1]:\n",
    "        #                 ans.append(i)\n",
    "        #             else:\n",
    "        #                 continue\n",
    "        #         else:\n",
    "        #             break\n",
    "        # return ans\n",
    "        dict1 = dict()\n",
    "        words.sort(key = len)\n",
    "        for i in words[0]:\n",
    "            dict1[i] = words[0].count(i)\n",
    "        for j in words:\n",
    "            for k in dict1.keys():\n",
    "                if j.count(k) < dict1[k]:\n",
    "                    dict1[k] = j.count(k)\n",
    "        ans = []\n",
    "        for i in dict1:\n",
    "            if dict1 == 0:\n",
    "                dict1.pop(i)\n",
    "            else:\n",
    "                for j in range(dict1[i]):\n",
    "                    ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        res = None\n",
    "        for a in words:\n",
    "            c = Counter(a)\n",
    "            if res is None:\n",
    "                res = c\n",
    "            else:\n",
    "                res &= c\n",
    "        return list(res.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # ans = []\n",
    "        # words.sort(key = len)\n",
    "        # for i in words[0]:\n",
    "        #     for j in words:\n",
    "        #         if words[0].count(i) <= j.count(i):\n",
    "        #             if j == words[-1]:\n",
    "        #                 ans.append(i)\n",
    "        #             else:\n",
    "        #                 continue\n",
    "        #         else:\n",
    "        #             break\n",
    "        # return ans\n",
    "        dict1 = dict()\n",
    "        words.sort(key = len)\n",
    "        for i in words[0]:\n",
    "            dict1[i] = words[0].count(i)\n",
    "        for j in words:\n",
    "            for k in dict1.keys():\n",
    "                if j.count(k) < dict1[k]:\n",
    "                    dict1[k] = j.count(k)\n",
    "        ans = []\n",
    "        for i in dict1:\n",
    "            if dict1 == 0:\n",
    "                dict1.pop(i)\n",
    "            else:\n",
    "                for j in range(dict1[i]):\n",
    "                    ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # 需要找到哪些字符是重复的，并且找到重复了多少次\n",
    "        dic = {}\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            unique_chars = set(words[i])\n",
    "            for value in unique_chars:\n",
    "                if value not in dic.keys():\n",
    "                    dic[value] = 1\n",
    "                else:\n",
    "                    dic[value] += 1\n",
    "\n",
    "        common_char = []\n",
    "        # 选择出现次数=n的字符\n",
    "        for key, value in dic.items():\n",
    "            if value == n:\n",
    "                common_char.append(key)\n",
    "        # 找到重复字符在各个元素中的出现次数的最小值\n",
    "\n",
    "        answer = []\n",
    "        for char in common_char:\n",
    "            cnt = 110\n",
    "            for j in range(n):\n",
    "                count1 = words[j].count(char)\n",
    "                cnt = min(count1, cnt)\n",
    "            answer.append((char, cnt))\n",
    "\n",
    "        # 得到最终答案\n",
    "        final_answer = []\n",
    "        for char, cnt in answer:\n",
    "            for i in range(cnt):\n",
    "                final_answer.append(char)\n",
    "        return final_answer\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        return list(reduce(lambda x, y: x & y, map(Counter, words)).elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        from collections import Counter\n",
    "        cou = Counter(words[0]) #第一个单词的字符计数\n",
    "        for i in words[1:]:\n",
    "            cou &= Counter(i)    #取交集\n",
    "        return list(cou.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        c1=Counter(words[0])\n",
    "        for i in words:\n",
    "            c2=Counter(i)\n",
    "            c1=c1&c2\n",
    "        l=[]\n",
    "        for i in c1:\n",
    "            for j in range(c1[i]):\n",
    "                l.append(i)\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        l=[]\n",
    "        d=Counter(words[0])\n",
    "        for i in range(1,len(words)):\n",
    "            d=Counter(words[i]) & d\n",
    "        for i in d:\n",
    "            while d[i]!=0:\n",
    "                l.append(i)\n",
    "                d[i]-=1\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        result = []\n",
    "        counter1 = Counter(words[0])\n",
    "        for i in counter1:\n",
    "            num = min(a.count(i) for a in words) \n",
    "\n",
    "            result+=i*num\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        hash_list=[0]*26\n",
    "        hash_other_list=[0]*26\n",
    "        for index,i in enumerate(words):\n",
    "            hash_other_list=[0]*26\n",
    "            for j in i:\n",
    "                hash_other_list[ord(j)-ord(\"a\")]+=1\n",
    "            if index==0:\n",
    "                hash_list=hash_other_list\n",
    "            else:\n",
    "                for k in range(26):\n",
    "                    hash_list[k]=min(hash_list[k],hash_other_list[k])\n",
    "        result=[]\n",
    "        for index,num in enumerate(hash_list):\n",
    "            while num!=0:\n",
    "                result.append(chr(index+ord(\"a\")))\n",
    "                num-=1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        l = [0]*26\n",
    "        for c in words[0]:\n",
    "            l[ord(c) - ord('a')] += 1\n",
    "        for i in range(1, len(words)):\n",
    "            ll = [0]*26\n",
    "            for c in words[i]:\n",
    "                ll[ord(c) - ord('a')] += 1\n",
    "            for j in range(26):\n",
    "                l[j] = min(l[j], ll[j])\n",
    "        re = []\n",
    "        # print(l)\n",
    "        for i in range(len(l)):\n",
    "            while l[i] != 0:\n",
    "                re.append(chr(ord('a') + i))\n",
    "                l[i] -= 1\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        result = Counter(words[0])\n",
    "        \n",
    "        for word in words[1:]:\n",
    "            result &= Counter(word)\n",
    "        return list(result.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        c1=Counter(words[0])\n",
    "        for i in words:\n",
    "            c2=Counter(i)\n",
    "            c1=c1&c2\n",
    "        l=[]\n",
    "        for i in c1:\n",
    "            l+=i*c1[i]\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def commonChars(self, words: List[str]) -> List[str]:\n",
    "#         tmp = collections.Counter(words[0])\n",
    "#         L=[]\n",
    "#         for i in range(1,len(words)):\n",
    "#             tmp = tmp & collections.Counter(words[i])\n",
    "#         for j in tmp:\n",
    "#             while tmp[j]>0:\n",
    "#                 L.append(j)\n",
    "#                 tmp[j] -=1\n",
    "#         return L\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        hash_0=[0]*26\n",
    "        result=[]\n",
    "        for c in words[0]:\n",
    "            hash_0[ord(c)-ord('a')] += 1\n",
    "        for i in range(1,len(words)):\n",
    "            hash_1=[0]*26\n",
    "            for c in words[i]:\n",
    "                hash_1[ord(c)-ord('a')]+=1\n",
    "            for j in range(26):\n",
    "                hash_0[j]=min(hash_0[j],hash_1[j])\n",
    "        for i in range(26):\n",
    "            while hash_0[i] >0:\n",
    "                result.append(chr(i+ord('a')))\n",
    "                hash_0[i] -=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        from collections import Counter\n",
    "        if len(words) == 1:\n",
    "            return list(words[0])\n",
    "        dic1 = Counter(words[0])\n",
    "        for word in words[1:]:\n",
    "            dic2 = Counter(word)\n",
    "            keys = set(dic1.keys()) & set(dic2.keys())\n",
    "            dic1 = {k:min(dic1[k],dic2[k]) for k in keys}\n",
    "        ans = []\n",
    "        for k,v in dic1.items():\n",
    "            ans.extend([k]*v)\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        hash = [0] * 26\n",
    "        for i in words[0]:\n",
    "            hash[ord(i)-ord('a')] += 1\n",
    "        for i in range(1, len(words)):\n",
    "            tmp_hash = [0]*26\n",
    "            for c in words[i]:\n",
    "                tmp_hash[ord(c)-ord('a')] += 1\n",
    "            \n",
    "            for i in range(26):\n",
    "                hash[i] = min(hash[i], tmp_hash[i])\n",
    "\n",
    "        result = []\n",
    "        for i, c in enumerate(hash):\n",
    "            while c > 0:\n",
    "                result.append(chr(i+ord('a')))\n",
    "                c -= 1\n",
    "        \n",
    "        return result\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        return list(reduce(operator.and_, map(Counter, words)).elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        cnts = [Counter(word) for word in words]\n",
    "        res_lst = []\n",
    "        for k, v in cnts[0].items():\n",
    "            min_common_num = v\n",
    "            flag = True\n",
    "            for cnt in cnts[1:]:\n",
    "                if k not in cnt:\n",
    "                    flag = False\n",
    "                    break\n",
    "                else:\n",
    "                    min_common_num = min(min_common_num, cnt[k])\n",
    "            if flag: res_lst.extend([k]*min_common_num)\n",
    "        return res_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        cnts = [Counter(word) for word in words]\n",
    "        res_lst = []\n",
    "        for k, v in cnts[0].items():\n",
    "            min_common_num = v\n",
    "            flag = True\n",
    "            for cnt in cnts[1:]:\n",
    "                if k not in cnt:\n",
    "                    flag = False\n",
    "                    break\n",
    "                else:\n",
    "                    min_common_num = min(min_common_num, cnt[k])\n",
    "            if flag: res_lst.extend([k]*min_common_num)\n",
    "        return res_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        key = set(words[0])\n",
    "        res = []\n",
    "\n",
    "        for k in key:\n",
    "            min_num = float('inf')\n",
    "            for i in range(len(words)):\n",
    "                w = words[i]\n",
    "                min_num = min(w.count(k), min_num)\n",
    "            res += min_num * k\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # res = []\n",
    "        \n",
    "        # key_character = set(words[0])\n",
    "\n",
    "        # for k_c in key_character:\n",
    "        #     min_k = float('inf')\n",
    "        #     for w in words:\n",
    "        #         min_k = min(w.count(k_c), min_k)\n",
    "        #     res += min_k * k_c\n",
    "        \n",
    "        # return res\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            \n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if not words: return []\n",
    "        result = []\n",
    "        hash = [0] * 26 # 用来统计所有字符串里字符出现的最小频率\n",
    "        for i, c in enumerate(words[0]):  # 用第一个字符串给hash初始化\n",
    "            hash[ord(c) - ord('a')] += 1\n",
    "        # 统计除第一个字符串外字符的出现频率\n",
    "        for i in range(1, len(words)):\n",
    "            hashOtherStr = [0] * 26\n",
    "            for j in range(len(words[i])):\n",
    "                hashOtherStr[ord(words[i][j]) - ord('a')] += 1\n",
    "            # 更新hash，保证hash里统计26个字符在所有字符串里出现的最小次数\n",
    "            for k in range(26):\n",
    "                hash[k] = min(hash[k], hashOtherStr[k])\n",
    "        # 将hash统计的字符次数，转成输出形式\n",
    "        for i in range(26):\n",
    "            while hash[i] != 0: # 注意这里是while，多个重复的字符\n",
    "                result.extend(chr(i + ord('a')))\n",
    "                hash[i] -= 1\n",
    "        return result\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        temp,all,result,res = [],{},[],[]\n",
    "        \n",
    "        result = list(set(words[0]).intersection(*words[1:]))\n",
    "        for char in result:\n",
    "            for i in range(len(words)):\n",
    "                temp.append(words[i].count(char))\n",
    "            all[char] = min(temp)\n",
    "            temp = []\n",
    "        print(all)\n",
    "        \n",
    "        for key in all.keys():\n",
    "            for p in range(0,all[key]):\n",
    "                res.append(key)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if not words:\n",
    "            return\n",
    "        hash = [0] * 26\n",
    "        # Initialize hash\n",
    "        for i, c in enumerate(words[0]):\n",
    "            hash[ord(c) - ord('a')] += 1\n",
    "\n",
    "        for i in range(1, len(words)):\n",
    "            hashOther = [0] * 26\n",
    "            for j in range(len(words[i])):\n",
    "                hashOther[ord(words[i][j]) - ord('a')] += 1\n",
    "            for k in range(26):\n",
    "                hash[k] = min(hash[k], hashOther[k])\n",
    "        result = []\n",
    "        for i in range(26):\n",
    "            while hash[i] != 0:\n",
    "                result.extend(chr(i + ord('a')))\n",
    "                hash[i] -= 1\n",
    "        return result                    \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if len(words) == 1: return []\n",
    "        res = list()\n",
    "        word = words[0]\n",
    "        left_words = [list(w) for w in words[1:] ]\n",
    "        for letter in word:\n",
    "            flag = True\n",
    "            for i in range(len(left_words)):\n",
    "                if letter not in left_words[i]: flag = False\n",
    "                else: \n",
    "                    left_words[i].remove(letter)\n",
    "            if flag: res.append(letter)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        n = len(words)\n",
    "        hax = Counter(words[0])\n",
    "        for x in range(1,n):\n",
    "            temp = Counter(words[x])\n",
    "            for y in hax:\n",
    "                hax[y] = min(hax[y], temp[y])\n",
    "        # return hax\n",
    "        res = [] \n",
    "        for x in hax:\n",
    "            res.extend([x] * hax[x])\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        n=len(words)\n",
    "        ht=[[0]*26 for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(len(words[i])):\n",
    "                index=ord(words[i][j])-ord('a')\n",
    "                ht[i][index]+=1\n",
    "        result=[]\n",
    "        for i in range(26):\n",
    "            cur=100\n",
    "            for j in range(n):\n",
    "                cur=min(cur,ht[j][i])\n",
    "            for j in range(cur):\n",
    "                result.append(chr(i+ord('a')))\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minfreq = [float(\"inf\")] * 26\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord(\"a\")] += 1\n",
    "            for i in range(26):\n",
    "                minfreq[i] = min(minfreq[i], freq[i])\n",
    "        \n",
    "        ans = list()\n",
    "        for i in range(26):\n",
    "            ans.extend([chr(i + ord(\"a\"))] * minfreq[i])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if len(words) == 1: return []\n",
    "        res = list()\n",
    "        word = words[0]\n",
    "        left_words = [list(w) for w in words[1:] ]\n",
    "        for letter in word:\n",
    "            flag = True\n",
    "            for i in range(len(left_words)):\n",
    "                if letter not in left_words[i]: flag = False\n",
    "                else: \n",
    "                    left_words[i].remove(letter)\n",
    "            if flag: res.append(letter)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        res =None\n",
    "        for a in words:\n",
    "            c= Counter(a)\n",
    "            if res is None:\n",
    "                res =c \n",
    "            else:\n",
    "                res &=c\n",
    "        return list(res.elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        from collections import Counter\n",
    "        words_count = Counter(words[0])\n",
    "        res = []\n",
    "        for i in range(1, len(words)):\n",
    "            other_count = Counter(words[i])\n",
    "            words_count = words_count & other_count\n",
    "        for i in words_count:\n",
    "            val = words_count[i]\n",
    "            while val:\n",
    "                res.append(i)\n",
    "                val -= 1\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minfreq = [float(\"inf\")] * 26\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord(\"a\")] += 1\n",
    "            for i in range(26):\n",
    "                minfreq[i] = min(minfreq[i], freq[i])\n",
    "        \n",
    "        ans = list()\n",
    "        for i in range(26):\n",
    "            ans.extend([chr(i + ord(\"a\"))] * minfreq[i])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        li, s, ans = list(), set(words[0]), list()\n",
    "        for x in words:\n",
    "            s = s & set(list(x))  # 交集\n",
    "        for c in s:\n",
    "            time = 101\n",
    "            for x in words:\n",
    "                time = min(x.count(c), time)\n",
    "            while time > 0:\n",
    "                ans.append(c)\n",
    "                time -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        dic = {}\n",
    "        for i in words[0]:\n",
    "            dic[i] = words[0].count(i)\n",
    "        for i in range(1,len(words)):\n",
    "            word = words[i]\n",
    "            temp = {}\n",
    "            for j in word:\n",
    "                temp[j] = word.count(j)\n",
    "            for char in dic:\n",
    "                if char in temp:\n",
    "                    dic[char] = min(dic[char],temp[char])\n",
    "                else:\n",
    "                    dic[char] = 0\n",
    "        res = []\n",
    "        for i in dic:\n",
    "            for _ in range(dic[i]):\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        # ans = []\n",
    "        # words.sort(key = len)\n",
    "        # for i in words[0]:\n",
    "        #     for j in words:\n",
    "        #         if words[0].count(i) <= j.count(i):\n",
    "        #             if j == words[-1]:\n",
    "        #                 ans.append(i)\n",
    "        #             else:\n",
    "        #                 continue\n",
    "        #         else:\n",
    "        #             break\n",
    "        # return ans\n",
    "        dict1 = dict()\n",
    "        words.sort(key = len)\n",
    "        for i in words[0]:\n",
    "            dict1[i] = words[0].count(i)\n",
    "        for j in words:\n",
    "            for k in dict1.keys():\n",
    "                if j.count(k) < dict1[k]:\n",
    "                    dict1[k] = j.count(k)\n",
    "        ans = []\n",
    "        for i in dict1:\n",
    "            if dict1 == 0:\n",
    "                dict1.pop(i)\n",
    "            else:\n",
    "                for j in range(dict1[i]):\n",
    "                    ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        minfreq = [float(\"inf\")] * 26\n",
    "        for word in words:\n",
    "            freq = [0] * 26\n",
    "            for ch in word:\n",
    "                freq[ord(ch) - ord('a')] += 1\n",
    "            for i in range(26):\n",
    "                minfreq[i] = min(minfreq[i], freq[i])\n",
    "        print(minfreq)\n",
    "        ans = list()\n",
    "        for i in range(26):\n",
    "            ans.extend([chr(i+ord('a'))] * minfreq[i])\n",
    "            print(ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        if not words:\n",
    "            return\n",
    "        hash = [0] * 26\n",
    "        result = []\n",
    "        for i, c in enumerate(words[0]):\n",
    "            hash[ord(c) - ord('a')] += 1\n",
    "\n",
    "        for i in range(1, len(words)):\n",
    "            hashOther = [0] * 26\n",
    "            for j in range(len(words[i])):\n",
    "                hashOther[ord(words[i][j]) - ord('a')] += 1\n",
    "            for k in range(26):\n",
    "                hash[k] = min(hash[k], hashOther[k])\n",
    "        for i in range(26):\n",
    "            while hash[i]:\n",
    "                result.extend(chr(i + ord('a')))\n",
    "                hash[i] -= 1\n",
    "        return result        \n",
    "                                    \n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        cnt=[0]*len(words)\n",
    "        res=[]\n",
    "        for i in range(len(words)):\n",
    "            cnt[i]=Counter(words[i])\n",
    "        for i in cnt[0]:\n",
    "            c=cnt[0][i]\n",
    "            for j in cnt:\n",
    "                try:\n",
    "                    c=min(c,j[i])\n",
    "                except:\n",
    "                    c=0\n",
    "                    break\n",
    "            res=res+[i]*c\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def commonChars(self, words):\n",
    "    \n",
    "    def count(w):\n",
    "      dict1 = defaultdict(int)\n",
    "      for ch in w:\n",
    "        dict1[ch] += 1\n",
    "      return dict1\n",
    "    \n",
    "    ans = []\n",
    "    arr = []\n",
    "    for w in words:\n",
    "      arr.append(count(w))\n",
    "      \n",
    "    s = set(words[0])\n",
    "    \n",
    "    def get_min(ch):\n",
    "      m = float('inf')\n",
    "      for d in arr:\n",
    "        m = min(m, d[ch])\n",
    "      return m  \n",
    "      \n",
    "    for ch in s:\n",
    "      t = get_min(ch)\n",
    "      ans += t * [ch]\n",
    "    \n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        char_count = [101]*26\n",
    "        for i, word in enumerate(words):\n",
    "            char_count_i = {}\n",
    "            for ch in word:\n",
    "                if ch not in char_count_i:\n",
    "                    char_count_i[ch] = 1\n",
    "                else:\n",
    "                    char_count_i[ch] += 1\n",
    "            for j in range(26):\n",
    "                ch = chr(j+97)\n",
    "                if ch not in char_count_i:\n",
    "                    char_count[j] = 0\n",
    "                else:\n",
    "                    char_count[j] = min(char_count[j], char_count_i[ch])\n",
    "        result = []\n",
    "        for i, count in enumerate(char_count):\n",
    "            if count==0:\n",
    "                continue\n",
    "            result.extend([chr(i+97)]*count)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonChars(self, words: List[str]) -> List[str]:\n",
    "        data = words[0]\n",
    "        s_list = []\n",
    "        j = 0\n",
    "        for i in data:\n",
    "            count = 999\n",
    "            for j in range(1, len(words)):\n",
    "                if i not in words[j]:\n",
    "                    j = -1\n",
    "                    break\n",
    "                else:\n",
    "                    count = min(count, words[j].count(i))\n",
    "            print(count)\n",
    "            if j != -1 and s_list.count(i) < count:\n",
    "                s_list.append(i)\n",
    "\n",
    "        return s_list\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
